//
//  YTVisitorView.swift
//  Rhythm_SinaWeibo
//
//  Created by Mr.Rhythm on 9/21/15.
//  Copyright © 2015 Mr.Rhythm. All rights reserved.
//

import UIKit
import SnapKit

class YTVisitorView: UIView {
    
    // MARK: - 判断是否为首页
    func setupInfo(imageName: String?, title: String) {
        YTMessageLabel.text = title
        
        if imageName != nil {
            YTIconView.image = UIImage(named: imageName!)
            YTHomeIconView.hidden = true
            sendSubviewToBack(YTMaskIconView)
        } else {
            starAnimation()
        }
    }
    
    private func starAnimation() {
        // 设置核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        // 使动画不会被销毁
        animation.removedOnCompletion = false
        
        YTIconView.layer.addAnimation(animation, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        // 注意!!! 此方法若执行,则程序会直接崩溃
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    private func setupUI() {
        
        // 1. 添加控件
        addSubview(YTIconView)
        addSubview(YTMaskIconView)
        addSubview(YTHomeIconView)
        addSubview(YTMessageLabel)
        addSubview(YTRegisterButton)
        addSubview(YTLoginButton)
        
        // 循环设定translatesAutoresizingMaskIntoConstraints属性为假
        for choose in subviews {
            choose.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 1> 图标
        YTIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        // 2> 小房子
        YTHomeIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(YTIconView.snp_centerX)
            make.centerY.equalTo(YTIconView.snp_centerY)
        }
        // 3> 消息文字
        YTMessageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(YTIconView.snp_centerX)
            make.top.equalTo(YTIconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(35)
        }
        // 4> 注册按钮
        YTRegisterButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(YTMessageLabel.snp_left)
            make.top.equalTo(YTMessageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        // 5> 登录按钮
        YTLoginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(YTMessageLabel.snp_right)
            make.top.equalTo(YTMessageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        // 6> 遮罩视图 VFL
        YTMaskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(YTRegisterButton.snp_bottom)
        }

        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
    // MARK: - 懒加载控件
    /// 图标 - 如果使用 image 创建 UIImageView，大小默认就是图像大小
    private lazy var YTIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮罩图片
    private lazy var YTMaskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子
    private lazy var YTHomeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 描述文字
    private lazy var YTMessageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    /// 注册按钮
    lazy var YTRegisterButton: UIButton = UIButton(title: "注册", fontSize: 14, color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    /// 登录按钮
    lazy var YTLoginButton: UIButton = UIButton(title: "登录", fontSize: 14, color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")

    }
